package com.example.back.service.impl;

import com.example.back.dto.req.BlacklistIpListsReq;
import com.example.back.dto.resp.BlacklistIpListsResp;
import com.example.back.jpa.AdminRepository;
import com.example.back.jpa.BlacklistIpRepository;
import com.example.back.mapper.BlacklistIpMapper;
import com.example.back.service.BlacklistIpService;
import com.example.back.utils.UserUtils;
import com.example.common.entity.Admin;
import com.example.common.entity.BlacklistIp;
import com.example.common.support.ApiException;
import com.example.common.utils.CheckUtils;
import com.example.common.utils.IdUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.javers.spring.annotation.JaversAuditable;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Date;

@Service
public class BlacklistIpServiceImpl implements BlacklistIpService {

    @Resource
    private BlacklistIpMapper blacklistIpMapper;

    @Resource
    private BlacklistIpRepository blacklistIpRepository;

    @Resource
    private AdminRepository adminRepository;

    @Resource
    private UserUtils userUtils;

    /**
     * 获取IP黑名单列表
     * @param req
     * @return
     */
    @Override
    public PageInfo<BlacklistIpListsResp> list(BlacklistIpListsReq req) {

        CheckUtils.checkNull(req, new ApiException(1, "对象不能为空"));
        //分页页码，默认1页
        int pageNumber = null == req.getPage() ? 1 : req.getPage();
        //分页条数，默认10条
        int pageSize = null == req.getLimit() ? 10 : req.getLimit();
        //黑名单IP
        String ipAddress = req.getIpAddress();
        return PageHelper
                .startPage(pageNumber, pageSize)
                .doSelectPageInfo(() -> blacklistIpMapper.findBlacklistIpAddress(ipAddress));
    }


    /**
     * 新增黑名单IP
     * @param req
     * @return
     */
    @Override
    @JaversAuditable
    public Object commitSave(BlacklistIpListsReq req) {

        CheckUtils.checkNull(req, new ApiException(1, "对象不能为空"));
        CheckUtils.checkNull(req.getIpAddress(), new ApiException(1, "请输入IP地址"));

        BlacklistIp obj = blacklistIpRepository.findByIpAddress(req.getIpAddress());
        if(obj!=null){
            throw new ApiException(1, "IP地址已存在");
        }

        Admin objs = adminRepository.findById(userUtils.getUser().getId()).orElse(null);

        BlacklistIp blacklistIp = new BlacklistIp();
        blacklistIp.setId(IdUtils.generateId());
        blacklistIp.setCreateTime(new Date());
        blacklistIp.setCreateName(objs.getUsername());
        blacklistIp.setIpAddress(req.getIpAddress());
        return blacklistIpRepository.save(blacklistIp);
    }


    /**
     * 删除IP黑名单
     * @param req
     * @return
     */
    @Override
    @JaversAuditable
    public Object commitDel(BlacklistIpListsReq req) {

        CheckUtils.checkNull(req, new ApiException(1, "对象不能为空"));

        BlacklistIp obj = blacklistIpRepository.findById(req.getId()).orElse(null);
        if(obj ==null){
            throw new ApiException(1, "IP地址不存在");
        }

        blacklistIpRepository.deleteById(obj.getId());

        return null;
    }
}
